/*
Copyright (c) 2009, Young Digital Planet SA
All rights reserved.

* Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this 
  list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, 
  this list of conditions and the following disclaimer in the documentation 
  and/or other materials provided with the distribution.
* Neither the name of the Young Digital Planet SA nor the names of its 
  contributors may be used to endorse or promote products derived from this 
  software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/
package pl.ydp.utils{
	import mx.styles.StyleManager;
	import mx.utils.StringUtil;
	
	import com.yauthor.utils.IPathBase;
	
	/**
	 * Narzędzia do operowaniu na stylach z CSS'a
	 */
	public class StylesUtil{
		
		/**
		 * Pattern do wykrywania liczby z końcówka px. np: 10px
		 * @private
		 */
		public static var PX_PATTERN:RegExp = /^(-?\d+)\s*px$/i;
		/**
		 * Pattern do rosparsowywania kolorów w postaci rgb(rrr,ggg,bbb)
		 * @private
		 */
		public static var RGB_PATTERN:RegExp = 
			/rgb\([ \t]*(\d{1,3})[ \t]*,[ \t]*(\d{1,3})[ \t]*,[ \t]*(\d{1,3})[ \t]*\)/i;
		/**
		 * Pattern do odnajdywania elementów kończących się na src
		 * @private
		 */
		public static var SRC_MATCH:RegExp = 
			/.*src$/i;
		/**
		 * Pattern do odczytywania urli
		 * @private
		 */
		public static var URL_PATTERN:RegExp = 
			/url\(\s*['"]?([^'"]*)['"]?\s*\)/i;
		
		/**
		 * Parsuje wartość pobraną z CSS'a.
		 * @param name nazwa wartości w CSS
		 * @param value wartość z CSS'a, może być to liczba, liczba+px, kolor,
		 * string lub uri w postaći url("..") (też nazwy kończące się na src
		 * są uznwana za uri)
		 * @return sparsowana wartość. Może to być string, number, boolean
		 */
		public static function parseValue( name:String, value:String, path:com.yauthor.utils.IPathBase ):* {			
			var trimmedValue:String = StringUtil.trim( value );
			var colorNumber:Number = StyleManager.getColorName( trimmedValue );
			var res:* = value;
			if (colorNumber != StyleManager.NOT_A_COLOR)
				res = colorNumber;
			else if ( !isNaN(Number(trimmedValue)) )
				res = Number(trimmedValue);
			else if ( trimmedValue.toLowerCase()=='false' )
				res = false;
			else if ( trimmedValue.toLowerCase()=='true' )
				res = true;
			else {
				var rsearch:Object = RGB_PATTERN.exec(trimmedValue);
				if ( rsearch!==null ) {
					res = Number(rsearch[1])*65536+
							Number(rsearch[2])*256+
							Number(rsearch[3]);
				} else {
					rsearch = PX_PATTERN.exec(trimmedValue);
					if ( rsearch!==null )
						res = Number(rsearch[1]);
					else {
						rsearch = URL_PATTERN.exec(trimmedValue);
						if ( rsearch!==null )
							res = path.resolvePath( StringUtil.trim( rsearch[1] ) );
						else
							res = trimmedValue;
					}
				}
			}
			
			return res;
		}
		
		/**
		 * Parses string array to array of style values. String jest rozdzielany na elementy
		 * i każdy element jest parsowany metodą parseValue i umieszczany w tablicy wynikowej
		 * @param value string zawierający listę wartości rozdzielonych przecinkami lub spacjami
		 * @pram path ścieżka bazowa dla url'i
		 * @return sparsowane wartości
		 * @see #parseValue()
		 */
		public static function parseArrayValue(value:String, path:com.yauthor.utils.IPathBase = null):Array{
			var arr:Array = new Array();
			var trimmedValue:String = StringUtil.trim(value);
			
			if(value == null)
				return arr; 			
			
			if(trimmedValue.indexOf(",") != -1){
				arr = trimmedValue.split(",");
			}else{
				arr = trimmedValue.split(" ");
			}
			
			for(var i:int = 0; i < arr.length; i++)
				arr[i] = parseValue(null, arr[i], path);
			
			return arr;
		}

	}
}